<template>
  <div class="app-container home">
    <!-- 搜索区域 - 修改为评论管理搜索项 -->
    <el-form :model="searchForm" class="search-form">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="题目名称">
            <el-input v-model="searchForm.questionName" placeholder="请输入题目名称" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="发布人名称">
            <el-input v-model="searchForm.publisherName" placeholder="请输入发布人名称" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="评论关键字">
            <el-input v-model="searchForm.commentKeyword" placeholder="请输入评论关键字" />
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="searchForm.timeRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="20">
          <!-- <el-button type="primary" @click="handleAdd">发布评论</el-button> -->
         
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 表格区域 - 修改为评论管理字段 -->
    <el-table
      ref="tableRef"
      :data="tableData"
      border
      stripe
      :row-key="row => row.id"
      @selection-change="handleSelectionChange"
    >
       <el-table-column type="selection" width="55" />
       <el-table-column label="题目名称" min-width="180">
        <template #default="scope">
          <el-link 
            type="primary"
            @click="handleDetail(scope.row)"
          >
            {{ scope.row.questionName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="publisherName" label="发布人名称" width="140" />
      <el-table-column prop="commentContent" label="评论内容" min-width="200" ellipsis  show-overflow-tooltip />
      <el-table-column prop="commentTime" label="评论时间" width="160" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <el-pagination
      class="pagination"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
      layout="prev, pager, next, jumper"
    />

    <!-- 新增批量操作栏 -->
    <div v-if="selectedRows.length > 0" class="batch-actions">
      <el-button type="danger" @click="handleBatchEdit">批量编辑</el-button>
      <el-button type="danger" @click="handleBatchDelete">批量删除</el-button>
      <el-button @click="handleCancelSelection">取消勾选</el-button>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, getCurrentInstance } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus';
import { useRouter } from 'vue-router'
const router = useRouter()
const { proxy } = getCurrentInstance()
// 搜索表单 - 适配评论管理字段
const searchForm = reactive({
  questionName: '',
  publisherName: '',
  commentKeyword: '',
  timeRange: []
})

// 表格数据 - 模拟评论数据
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const selectedRows = ref([])

// 模拟评论数据
let mockData = Array.from({ length: 50 }, (_, i) => ({
  id: i + 1,
  questionName: `题目${i + 1}`,
  publisherName: `用户${i % 10 + 1}`,
  commentContent: `这是关于题目${i + 1}的评论内容${i + 1}`,
  commentTime: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000).toLocaleDateString()
}))

// 搜索方法 - 适配评论字段过滤
const handleSearch = () => {
  const filtered = mockData.filter(item => {
    const matchesQuestion = item.questionName.includes(searchForm.questionName)
    const matchesPublisher = item.publisherName.includes(searchForm.publisherName)
    const matchesKeyword = item.commentContent.includes(searchForm.commentKeyword)
    const matchesTimeRange = searchForm.timeRange.length ? 
      new Date(item.commentTime) >= new Date(searchForm.timeRange[0]) && 
      new Date(item.commentTime) <= new Date(searchForm.timeRange[1]) : true
    return matchesQuestion && matchesPublisher && matchesKeyword && matchesTimeRange
  })

  total.value = filtered.length
  tableData.value = filtered.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value)
}

// 重置方法
const handleReset = () => {
  searchForm.questionName = ''
  searchForm.publisherName = ''
  searchForm.commentKeyword = ''
  searchForm.timeRange = []
  currentPage.value = 1
  handleSearch()
}

// 分页变化
const handlePageChange = (page) => {
  currentPage.value = page
  handleSearch()
}

const handleSizeChange = (size) => {
  pageSize.value = size
  currentPage.value = 1
  handleSearch()
}

// 选择变化
const handleSelectionChange = (rows) => {
  selectedRows.value = rows
}

// 编辑处理 --- 单点编辑
// 编辑评论 - 跳转到编辑页面
const handleEdit = (row) => {
  router.push(`/comment/edit/${row.id}`)
}

const handleBatchEdit  = () => {
  router.push({ path: '/comment/batchEdit', query: { ids: selectedRows.value.map(row => row.id).join(',') } })
}


function handleAdd() {
  router.push({ path: '/comment/add' })
}
const handlePreview = (row) => {
  // 实际项目中这里应该跳转到预览页面
  router.push({ path: '/scoringManage/scoring', query: { id: row.id } })
}

// 删除处理
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除该评论吗？', '确认删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    mockData = mockData.filter(item => item.id !== id)
    handleSearch()
    ElMessage({ message: '删除成功', type: 'success' })
  }).catch(() => {})
}

// 批量删除
const handleBatchDelete = () => {
  if (selectedRows.value.length === 0) return
  ElMessageBox.confirm(`确定要删除选中的${selectedRows.value.length}条评论吗？`, '确认删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const ids = selectedRows.value.map(row => row.id)
    mockData = mockData.filter(item => !ids.includes(item.id))
    selectedRows.value = []
    handleSearch()
    ElMessage({ message: '批量删除成功', type: 'success' })
  }).catch(() => {})
}

// 取消勾选
const handleCancelSelection = () => {
  selectedRows.value = []
  proxy.$refs.tableRef.clearSelection()
}

// 详情处理
const handleDetail = (row) => {
  ElMessage({ message: `查看评论详情: ${row.questionName}`, type: 'info' })
  // 实际项目中这里应该跳转到详情页面
}

// 初始化加载数据
handleSearch()




</script>

<style scoped>
/* 保持与知识点管理页面相同的样式 */
.search-form {
  margin-bottom: 20px;
  padding: 15px;
  background: #f5f7fa;
  border-radius: 4px;
}

.search-actions {
  display: flex;
  gap: 10px;
  margin-top: 22px;
}

.pagination {
  margin-top: 15px;
  text-align: right;
}

.batch-actions {
  margin-top: 10px;
  display: flex;
  gap: 10px;
}
</style>